//
//  CZStatusViewModel.swift
//  01-sinaWeibo-05-09
//
//  Created by mac on 16/5/24.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class CZStatusViewModel: NSObject {

    //单例
    static let sharedInstance: CZStatusViewModel = CZStatusViewModel()
    
    private override init() {
        super.init()
    }
    
    
    //加载模型数据
    func loadStaus(loadStatusCallback:(status:[CZStatus]?,error:NSError?)->()){
    
        guard let access_token = CZUserAccountViewModel.sharedInstance.userAccount?.access_token else{
        
            CZPrint(items: "access_token没有值,不去请求微博数据")
            return
        }
        
        let urlString = "2/statuses/home_timeline.json"
        
        //参数
        let parameters = [
             "access_token" : access_token
        ]
        
        CZNetworkTool.sharedInstance.request(RequestMethod.GET, URLSTring: urlString, parameters: parameters, progress:nil , success: { (_, responseObject) -> Void in
//            CZPrint(items: "获取数据成功:\(responseObject)")
            
            //true false
            if let result = responseObject as? [String : AnyObject]{
            
                if let statuses = result["statuses"] as? [[String : AnyObject]]{
                
                    //创建一个空数组 来存放遍历好的微博属性
                  var statusArray = [CZStatus]()
                    for dict in statuses {
                    
                        //字典转模型
                        let statusModel = CZStatus(dict: dict)
                        CZPrint(items: "statusModel ==== :::\(statusModel)")
                        //将模型添加到数组中
                        statusArray.append(statusModel)
                        
                        
                   
                    }
                    //                    CZPrint(items: "微博模型数组: \(statusArray)")
                    // 将微博数据告诉homeViewController(调用的人)
                    loadStatusCallback(status:statusArray, error:nil)
                }
                
                
               
            }
            
            
            }) { (_, error) -> Void in
                CZPrint(items: "获取数据失败\(error)")
        }
        
        
        
        
    }
    
}
